<?php
/**
 * @author Ngo Minh Tri <minhtri2711@gmail.com>
 */
require_once APPPATH . "controllers/cmsbase.php";
class cms_site_frame extends CmsBase {

	var $_require_login = true;
	var $controller_js_path = 'cms_site_frame/main';

	function __construct() {
		parent :: __construct();
		$this->load->model('model_c1', 'model_c1');
		$this->load->model('model_nav', 'model_nav');
	}

	/**
	 *
	 *
	 */
	function add($level = 1, $nav_id = 0) {
		$this->load->model("Model_role", "model_role");
		$level = intval($level) ? intval($level) : 1;
		$this->_level = $level;
		$this->_nav_id = $nav_id;
		$roles = $this->model_role->fetch_all();
		$layouts = $this->_settings['layout'];
		$layouts = json_decode($layouts, true);

		if(!empty($_POST['save'])){
			$_SESSION['flash'] = time();
			$data = parse_input("item_", $_POST);
			// Get user info
			$user_id = $_SESSION[ADMINUSERLOGIN]['id'];
			$data['owner_id'] = $user_id;
			$data['user_id'] = $user_id;

			$nav_id = $this->model_nav->insert($data);
			// Duplicate table

			// Redirect to page template list
			redirect(site_url(array('c' => 'cms_site_content', 'm' => 'index', 'level' => $level + 1, 'nav_id' => $nav_id)), "refresh");
		}
		else{
			$ed['pid'] = $nav_id;
			$ed['level'] = $level;
			return $this->_render_my_view(array ('link' => '', 'level' => $level, 'ed' => $ed, 'roles' => $roles, 'layouts' => $layouts));
		}
	}

	/**
	 *
	 *
	 */
	function edit($level = 1, $nav_id = 0) {
		$this->load->model("Model_role", "model_role");
		$level = intval($level) ? intval($level) : 1;
		$this->_level = $level;
		$this->_nav_id = $nav_id;
		$roles = $this->model_role->fetch_all();
		$layouts = $this->_settings['layout'];
		$layouts = json_decode($layouts, true);

		if(!empty($_POST['save']) && intval($nav_id)){
			$_SESSION['flash'] = time();
			$data = parse_input("item_", $_POST);
			// Get user info
			$user_id = $_SESSION[ADMINUSERLOGIN]['id'];
			$data['user_id'] = $user_id;

			$this->model_nav->update($this->_nav_id, $data);
			// Redirect to page template list
			redirect(site_url(array('c' => 'cms_site_content', 'm' => 'index', 'level' => $level, 'nav_id' => $this->_nav_id)), "refresh");
		}
		else{
			// nav information;
			$ed = $this->model_nav->find($this->_nav_id);
			return $this->_render_my_view(array ('level' => $level, 'nav_id' => $this->_nav_id, 'ed' => $ed,  'roles' => $roles, 'layouts' => $layouts));
		}
	}

	/**
	 *
	 *
	 */
	function delete($level = 0, $nav_id = 0) {
		$node_del = $this->model_nav->find($nav_id);
		$pid = $node_del['pid'];
//		$root_navs = $this->model_nav->findTree($level, $nav_id, $nav_id);
//		echo "<pre>";
//		var_dump($root_navs);exit;
//		echo "</pre>";
		// Delete tree
		$this->model_nav->delete($nav_id);
		// Redirect to home
		redirect(site_url(array('c' => 'cms_site_content', 'm' => 'index', 'level' => $level-1, 'nav_id' => $pid)), "refresh");
	}

	/**
	 *
	 *
	 */
	function sortby($level = 0, $nav_id = 0){
		$this->_level = $level;
		$this->_nav_id = $nav_id;
		if($_SERVER['REQUEST_METHOD'] === 'POST'){
			$ids = $this->input->post('navIds');
			if(!empty($ids)){
				$i = 1;
				foreach($ids as $val){
					$this->model_nav->update($val, array('sort' => $i));
					$i++;
				}
				$result = array('status' => 'success', 'message' => 'You have just update items\'s position');
				echo json_encode($result);
				exit;
			}
		}
		else{
			// Get Parent Id
			$pid = 0;
			if(intval($nav_id)){
				$item = $this->model_nav->find($nav_id);
				$pid = $item['pid'];
			}
			$navs = $this->model_nav->findnav(array("level" => $level - 1, "pid" => $pid));
			return $this->_render_my_view(array ('navs' => $navs, 'level' => $level, 'nav_id' => $nav_id));
		}
	}

	/**
	 *
	 *
	 */
	function preview($level=0, $nav_id = 0){
		$this->_level = $level;
		$this->_nav_id = $nav_id;
		//redirect(site_url(array('c' => 'home', 'm' => 'index','nav_id' => $this->_nav_id)),"refesh");
		redirect(base_url()."index.php/home/index/$nav_id");
	}

	/**
	 *
	 *
	 */
	function preview_node($level=0,$nav_id=0, $data){
		$this->load->view($this->module_name . '/' . $this->controller_name . '/' ."preview_content", array('data'=>$data));
	}
}
?>
